<template>
  <div class="dashboard-container">
    <div class="app-container" />
    <!-- 卡片布局 -->
    <el-card class="box-card">
      <!-- tabs标签栏布局 -->
      <el-tabs v-model="activeName">
        <el-tab-pane label="组织架构" name="first" class="tab-pane">
          <!-- layout栅格布局 绘制头部-->
          <el-row
            type="flex"
            class="department-header"
            justify="space-between"
            align="middle"
          >
            <!-- 左侧布局 -->
            <el-col :span="20">
              <i class="el-icon-s-home" />
              <span class="company">江苏传智播客教育科技股份有限公司</span>
            </el-col>
            <!-- 右侧 -->
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <el-col>负责人</el-col>
                <el-col>
                  <el-dropdown>
                    <span class="el-dropdown-link">
                      操作<i class="el-icon-arrow-down el-icon--right" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="dialogShowFn('')">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="editShowFn(data.id)">编辑部门</el-dropdown-item>
                      <el-dropdown-item @click.native="delDeptsFn(data.id)">删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
          <el-tree
            :data="treeData"
            :props="defaultProps"
            :default-expand-all="true"
            class="departments-tree"
          >
            <!-- 使用插槽自定义结构 -->
            <!-- 插槽本身提供了两个数据，data当前行的数据 -->
            <template v-slot="{ data }">
              <el-row
                style="width: 100%"
                type="flex"
                justify="space-between"
                align="middle"
              >
                <!-- 左侧布局 -->
                <el-col :span="20">
                  <span>{{ data.name }}</span>
                </el-col>
                <!-- 右侧 -->
                <el-col :span="4">
                  <el-row type="flex" justify="end">
                    <el-col>{{ data.manager }}</el-col>
                    <el-col>
                      <el-dropdown>
                        <span class="el-dropdown-link">
                          操作<i class="el-icon-arrow-down el-icon--right" />
                        </span>
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="dialogShowFn(data.id)">添加子部门</el-dropdown-item>
                          <el-dropdown-item @click.native="editShowFn(data.id)">编辑部门</el-dropdown-item>
                          <el-dropdown-item v-if="data&&!data.children" @click.native="delDeptsFn(data.id)">删除部门</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-tree>
        </el-tab-pane>
      </el-tabs>
      <el-dialog
        :title="isEdit?'编辑组织架构':'新增组织架构'"
        :visible.sync="dialogVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="50%"
      >
        <!-- 弹出层插件（只包含弹出层内的内容） -->
        <dept-dialog
          v-if="dialogVisible"
          :parent-id="parentId"
          :is-edit="isEdit"
          :origin-list="originList"
          @updata-list="updataListFn"
          @close="closeFn"
        />
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getDepartmentsAPI, delDeptsAPI } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import DeptDialog from './deptsDialog.vue'
export default {
  components: {
    DeptDialog
  },
  data() {
    return {
      activeName: 'first',
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      dialogVisible: false,
      parentId: '',
      isEdit: false, // 是否为编辑状态
      originList: []
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 删除部门
    async delDeptsFn(id) {
      // 点击时触发询问框
      const confirmRes = await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 如果选择的是取消，返回并提示
      if (confirmRes === 'cancel') return this.$message('您取消了删除')
      // 将要删除的id传值
      const res = await delDeptsAPI(id)
      this.$message(res.message)
      this.getDepartments()
    },
    // 点击编辑
    editShowFn(id) {
      this.parentId = id
      this.dialogVisible = true
      this.isEdit = true
    },
    closeFn() {
      this.dialogVisible = false
    },
    updataListFn() {
      // 提交表单之后，重新渲染页面，将提交后的新数据渲染到页面中
      this.getDepartments()
      this.dialogVisible = false
    },
    dialogShowFn(id) {
      this.parentId = id
      this.dialogVisible = true
      this.isEdit = false
    },
    async getDepartments() {
      const res = await getDepartmentsAPI()
      // console.log(res)
      // this.treeData = res.data.depts
      this.originList = res.data.depts.map(item => ({
        name: item.name,
        id: item.id,
        pid: item.pid,
        code: item.code
      }))
      this.treeData = tranListToTreeData(res.data.depts)
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
.departments-tree {
  margin: 12px 0px;
}
</style>
